﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Data;
using App.Domain.Contracts;
using App.Model;
using System.Data.Objects.SqlClient;
using System.Data.Objects;
using App.Domain.Models;

namespace App.Domain.Handlers
{
    public class GetExpiringPosition
    {
        private readonly IJobPositionRepository _jobPositionRepository;
        private readonly ILovRepository _lovRepository;

        public GetExpiringPosition(IJobPositionRepository jobPositionRepository, ILovRepository lovRepository)
        {
            _jobPositionRepository = jobPositionRepository;
            _lovRepository = lovRepository;
        }

        public IEnumerable<ExpiringPositionModel> Execute(Guid campanyid)
        {
            try
            {
                var list = from a in _jobPositionRepository.GetAll().Where(i => i.CompanyId == campanyid
                           && SqlFunctions.DateDiff("day", DateTime.Now, i.EndDate) <=10
                           && SqlFunctions.DateDiff("day", DateTime.Now, i.EndDate) > 0)
                           join b in _lovRepository.GetAll()
                           on a.Province  equals b.Value into c
                           from d in c.DefaultIfEmpty()
                           join f in _lovRepository.GetAll()
                           on a.City  equals f.Value into g
                           from h in g.DefaultIfEmpty()
                           select new ExpiringPositionModel()
                           {
                               JobPositionId = a.Id,
                               JobTitle = a.JobTitle,
                               Place = d.Name + h.Name,
                               EndDate = a.EndDate,
                               ViewCount = 0,
                               ClickCount = 0,
                               ApplyCount = 0,
                           };
                var l = _jobPositionRepository.GetAll().Where(i => i.CompanyId == campanyid
                           && SqlFunctions.DateDiff("day", DateTime.Now, i.EndDate) <= 100
                    //&& SqlFunctions.DateDiff("day", DateTime.Now, i.EndDate) > 0
                           );

                return list;

            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToRetrieveReminderExceptionMessage", ex);
            }
        }
    }
}
